/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package usuario;

/**
 *
 * @author Deico
 */

import conexion.DB;
import java.util.Date;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import javax.swing.JOptionPane;

public class documentos {
    
    public static void nuevaReceta(String from,String receta,String to,java.util.Date fecha,String asunto){
        
        try {

            PreparedStatement prep = DB.DB_CONNECTION.prepareStatement(
           "insert into recetas (`from`,contenido,`to`,fecha,asunto) values (?,?,?,?,?);");
             
            prep.setString(1,from);
            prep.setString(2,receta);
            prep.setString(3,to);
            prep.setDate(4,new java.sql.Date(fecha.getTime()));
            prep.setString(5,asunto);
            
            prep.executeUpdate();
            
            } catch (SQLException ex) {
            System.out.println(ex);
        }
        
    }
    
    public static  ArrayList<Object[]> getListaRecetas(String user){
        

         ArrayList<Object[]> lista = new ArrayList();
         
        try {

          java.sql.Statement stat = DB.DB_CONNECTION.createStatement();
          ResultSet rs = stat.executeQuery("SELECT * FROM recetas where recetas.`to`='"+user+"';");
          while (rs.next()) {
          
              lista.add(new Object[]{rs.getInt("id"),rs.getString("from"),rs.getString("contenido"),
                  rs.getString("to"),rs.getDate("fecha"),rs.getDate("asunto")
              });
              
          }
          rs.close();
          

         } catch (SQLException ex) { 
         JOptionPane.showMessageDialog(null, ex, "Error", JOptionPane.ERROR_MESSAGE);
         }
        
        return lista;
    }
    
     public static void nuevoExamen(String from,String examen,String to,java.util.Date fecha,String asunto){
        
        try {

            PreparedStatement prep = DB.DB_CONNECTION.prepareStatement(
           "insert into examenes (`from`,contenido,`to`,fecha,asunto) values (?,?,?,?,?);");
             
            prep.setString(1,from);
            prep.setString(2,examen);
            prep.setString(3,to);
            prep.setDate(4,new java.sql.Date(fecha.getTime()));
            prep.setString(5,asunto);
            prep.executeUpdate();
            
            } catch (SQLException ex) {
            System.out.println(ex);
        }
        
    }
    
    public static  ArrayList<Object[]> getListaExamenes(String user){
        

         ArrayList<Object[]> lista = new ArrayList();
         
        try {

          java.sql.Statement stat = DB.DB_CONNECTION.createStatement();
          ResultSet rs = stat.executeQuery("SELECT * FROM examenes where examenes.`to`='"+user+"';");
          while (rs.next()) {
          
              lista.add(new Object[]{rs.getInt("id"),rs.getString("from"),rs.getString("contenido"),
                  rs.getString("to"),rs.getDate("fecha"),rs.getDate("asunto")
              });
              
          }
          rs.close();
          

         } catch (SQLException ex) { 
         JOptionPane.showMessageDialog(null, ex, "Error", JOptionPane.ERROR_MESSAGE);
         }
        
        return lista;
    }
    
    public static void nuevaLicencia(String from,String licencia,String to,java.util.Date fecha,String asunto){
        
        try {

            PreparedStatement prep = DB.DB_CONNECTION.prepareStatement(
           "insert into licencias (`from`,contenido,`to`,fecha,asunto) values (?,?,?,?,?);");
             
            prep.setString(1,from);
            prep.setString(2,licencia);
            prep.setString(3,to);
            prep.setDate(4,new java.sql.Date(fecha.getTime()));
            prep.setString(5,asunto);
            prep.executeUpdate();
            
            } catch (SQLException ex) {
            System.out.println(ex);
        }
        
    }
    
    public static  ArrayList<Object[]> getListaLicencias(String user){
        

         ArrayList<Object[]> lista = new ArrayList();
         
        try {

          java.sql.Statement stat = DB.DB_CONNECTION.createStatement();
          ResultSet rs = stat.executeQuery("SELECT * FROM licencias where licencias.`to`='"+user+"';");
          while (rs.next()) {
          
              lista.add(new Object[]{rs.getInt("id"),rs.getString("from"),rs.getString("contenido"),
                  rs.getString("to"),rs.getDate("fecha"),rs.getDate("asunto")
              });
              
          }
          rs.close();
          

         } catch (SQLException ex) { 
         JOptionPane.showMessageDialog(null, ex, "Error", JOptionPane.ERROR_MESSAGE);
         }
        
        return lista;
    }
}
